import {createAsyncThunk, createSlice} from "@reduxjs/toolkit";
import axios from "axios";
import store from "../index";

/**
 *
 * 参数1: payload
 * 参数2 ： store
 */
export const fetchBannerList = createAsyncThunk("fetch/banners",async (payload,store) => {
    console.log(payload,"chunkApi",store)
    let res = await axios.get("http://123.207.32.32:8000/home/multidata")
    let banners = res.data.data.banner.list
    store.dispatch(changeBanners(banners))
    // return banners
})

const homeSlice = createSlice({
    name: "home",
    initialState: {
        bannerList: []
    },
    reducers:{
      changeBanners(state,{ payload}){
          state.bannerList = payload
      }
    },

    // reducers:(create)=>{
    //     changeBanners:create.reducer(()=>{
    //
    //     }),
    //         fetchBannerList:create.asyncThunk(()=>{
    //
    //     },{})
    // },

    // extraReducers:(builder)=>{
    //     builder.addCase(fetchBannerList.pending,(state)=>{
    //         console.log("pending",state)
    //     })
    //     builder.addCase(fetchBannerList.fulfilled,(state, action)=>{
    //         // 返回的结果，， 会在action的payload中
    //         state.bannerList = action.payload
    //         console.log("fullfiled",action)
    //     })
    // }
})


export const {changeBanners} = homeSlice.actions


export default homeSlice.reducer
